package com.macro.mall.product.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.PmProdSpec;
import com.macro.mall.model.PmProduct;
import com.macro.mall.model.PmPrpty;
import com.macro.mall.product.dto.*;
import com.macro.mall.product.service.PmProdSpecService;
import com.macro.mall.product.service.PmProdStockService;
import com.macro.mall.product.service.PmProductService;
import com.macro.mall.product.util.DozerUtils;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 */
@Slf4j
@Controller
@Api(tags = "AppProdSpecController", description = "App服务接口")
@RequestMapping("/appProdSpec")
public class AppProdSpecController {

    @Autowired
    private PmProdSpecService pmProdSpecService;
    @Autowired
    private PmProdStockService pmProdStockService;
    @Autowired
    private PmProductService pmProductService;
    @Autowired
    private Mapper mapper;

    @ApiOperation(value = "APP服务列表")
    @RequestMapping(value = "/listPage", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmProdSpecDto>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "searchName", defaultValue = "") String searchName
    ) {
        Map<String,String> searchMap = new HashMap<String,String>();
        if(!StringUtils.isBlank(searchName)){
            searchName= URLDecoder.decode(searchName);
            searchMap.put("searchName",searchName);
        }
        List<PmProdSpec> prodSpecList = pmProdSpecService.listPageApp(pageNum,pageSize,searchMap);
        PageInfo<PmProdSpec> pageInfo = new PageInfo<PmProdSpec>(prodSpecList);
        List<PmProdSpecDto> dtoList  = pmProdSpecService.converDataList(prodSpecList);
        CommonPage<PmProdSpecDto> result = new CommonPage<PmProdSpecDto>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setTotalPage(pageInfo.getPages());
        result.setList(dtoList);
        return CommonResult.success(result);
    }
    @ApiOperation(value = "APP单个服务简介")
    @RequestMapping(value = "/getSpecDtail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmProdSpecDetailDto> getSpecDtail(@RequestParam(value = "prodSpecId") Long prodSpecId
    ) {
        PmProdSpecDetailDto  detailDto =pmProdSpecService.getSpecDetailDtoBySpecId(prodSpecId);
        return CommonResult.success(detailDto);
    }
    @ApiOperation(value = "APP根据品类包获取已发布商品属性集合")
    @RequestMapping(value = "/getSpecPrpty", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmPrptyDto>> getSpecPrpty(@RequestParam(value = "packageId") String packageId,
                                                       @RequestParam(value = "prodSpecId") Long prodSpecId
    ) {
        List<PmPrpty> prptyList = new ArrayList<PmPrpty>();
        if(!StringUtils.isBlank(packageId) && prodSpecId!=null && prodSpecId>0){
            prptyList=pmProdSpecService.getSpecPrptyBySelective(packageId,prodSpecId);
        }
        List<PmPrptyDto> dtoList = DozerUtils.mapList(mapper, prptyList, PmPrptyDto.class);
        return CommonResult.success(dtoList);
    }
    @ApiOperation(value = "APP根据选择属性查询满足条件场馆的地点")
    @RequestMapping(value = "/getProdSite", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<Map<String,Object>>> getProdSite(@RequestBody QueryProdSiteDto queryDto, BindingResult result
    ) {
        if(StringUtils.isBlank(queryDto.getSalePrptyArray())){
            return CommonResult.failed("属性不能为空！");
        }
        if(queryDto.getProdSpecId()==null ||queryDto.getProdSpecId()<1){
            return CommonResult.failed("服务编码不能为空！");
        }
        JSONArray salePrptyArrayChg=null;
        try {
            salePrptyArrayChg = JSON.parseArray(queryDto.getSalePrptyArray());
        }catch (Exception e){
            log.error("获取场馆信息失败：销售属性查询条件转换失败！"+e.getMessage());
            return  CommonResult.failed("获取场馆信息失败：销售属性查询条件转换失败！"+e.getMessage());
        }
        JSONArray naturePrptyArrayChg=null;
        if(!StringUtils.isBlank(queryDto.getNaturePrptyArray())){
            naturePrptyArrayChg=JSON.parseArray(queryDto.getNaturePrptyArray());
        }

        List<Map<String,Object>> resultList = pmProdSpecService.getSpecChannelBySelective(salePrptyArrayChg,naturePrptyArrayChg, queryDto.getProdSpecType(), queryDto.getProdSpecId());

        //根据经纬度计算距离 并排序
        if(resultList!=null && resultList.size()>0 && !StringUtils.isBlank(queryDto.getLatitude()) &&!StringUtils.isBlank(queryDto.getLongitude())){
            pmProdSpecService.sortSpecChannel(resultList,queryDto.getLatitude(),queryDto.getLongitude());
        }

        return CommonResult.success(resultList);
    }
    @ApiOperation(value = "APP获取库存")
    @RequestMapping(value = "/getServerStock", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ReturnServiceStockDto> getServerStock(@RequestParam(value = "prodId") Long prodId,
                                                              @RequestParam(value = "skuInstId") Long skuInstId,
                                                              @RequestParam(value = "siteId") Integer siteId
    ) {
        if(prodId==null ||prodId<1){
            return CommonResult.failed("商品编码不能为空！");
        }
        if(skuInstId==null ||skuInstId<1){
            return CommonResult.failed("skuInstId不能为空！");
        }
        if(siteId==null ||siteId<1){
            return CommonResult.failed("店面编码不能为空！");
        }

        ReturnServiceStockDto returnDto =pmProdStockService.getServerStock(prodId,skuInstId,siteId);

        return CommonResult.success(returnDto);
    }
    @ApiOperation(value = "获取单个产品")
    @RequestMapping(value = "/getProductById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmProductDto> getProductById(@RequestParam(value = "prodId", defaultValue = "") Long prodId
    ) {
        PmProduct pmProduct = pmProductService.getById(prodId);
        PmProductDto pmProductDto= pmProductService.converDataInfo(pmProduct);
        return CommonResult.success(pmProductDto);
    }
    @ApiOperation(value = "APP获取库存-上门服务")
    @RequestMapping(value = "/getServerStockByDoor", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ReturnServiceStockDto> getServerStockByDoor(@RequestBody QueryStockDoorDto queryDto, BindingResult result
    ) {

        ReturnServiceStockDto returnDto =pmProdStockService.getServerStockByDoor(queryDto);

        return CommonResult.success(returnDto);
    }
    @ApiOperation(value = "根据商品规格标识获取单个商品规格")
    @RequestMapping(value = "/getProdSpecById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<AppProdSpecDto> getProdSpecById(@RequestParam(value = "prodSpecId", defaultValue = "") Long prodSpecId
    ) {
        AppProdSpecDto prodSpec = pmProdSpecService.getByIdApp(prodSpecId);
        return CommonResult.success(prodSpec);
    }
    @ApiOperation(value = "根据商品标识获取商品规格")
    @RequestMapping(value = "/getProdSpecByProdId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmProdSpec> getProdSpecByProdId(@RequestParam(value = "prodId", defaultValue = "") Long prodId
    ) {
        PmProdSpec prodSpec = pmProductService.getProdSpecByProdId(prodId);
        return CommonResult.success(prodSpec);
    }
}
